package com.neuedu.servlet;

import com.google.gson.Gson;
import com.neuedu.pojo.Food;
import com.neuedu.pojo.PageResult;
import com.neuedu.service.FoodService;
import com.neuedu.service.Imp.FoodServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/food")
public class FoodServlet extends HttpServlet {
    private FoodService foodService = new FoodServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        switch (op) {
            case "get":
                getFood(req, resp);
                break;
            case "add":
                addFood(req, resp);
                break;
            case "update":
                updateFood(req, resp);
                break;
            case "del":
                delFood(req, resp);
                break;
            default:
                resp.getWriter().write(0+"");
                break;
        }
    }

    public void getFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String foodName = req.getParameter("foodName");
        String categoryId = req.getParameter("categoryId");
        String shopId = req.getParameter("shopId");

        PageResult<List<Food>> pageResult = foodService.getFood(currentPage,pageSize,foodName,categoryId,shopId);

        String json = new Gson().toJson(pageResult);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    public void addFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String foodName = req.getParameter("foodName");
        String foodExplain = req.getParameter("foodExplain");
        String foodImg = req.getParameter("foodImg");
        String foodPrice = req.getParameter("foodPrice");
        String shopId = req.getParameter("shopId");
        String remarks = req.getParameter("remarks");
        String stock = req.getParameter("stock");
        String status = req.getParameter("status");
        String categoryId = req.getParameter("categoryId");
        String recommend = req.getParameter("recommend");
        String hot = req.getParameter("hot");

        Integer i = foodService.addFood(foodName,foodExplain,foodImg,foodPrice,shopId,remarks,stock,status,categoryId,recommend,hot);

        resp.getWriter().write(i + "");
    }

    public void updateFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String foodName = req.getParameter("foodName");
        String foodExplain = req.getParameter("foodExplain");
        String foodImg = req.getParameter("foodImg");
        String foodPrice = req.getParameter("foodPrice");
        String remarks = req.getParameter("remarks");
        String stock = req.getParameter("stock");
        String status = req.getParameter("status");
        String categoryId = req.getParameter("categoryId");
        String foodId = req.getParameter("foodId");

        Integer i = foodService.updateFood(foodName,foodExplain,foodImg,foodPrice,remarks,stock,status,categoryId,foodId);

        resp.getWriter().write(i + "");
    }

    public void delFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String foodId = req.getParameter("foodId");

        Integer i = foodService.delFood(foodId);

        resp.getWriter().write(i + "");
    }
}
